package fule.com.webviewdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import fule.com.webviewdemo.R;
import fule.com.webviewdemo.util.GlideUtils;

/**
 * 作者： njb
 * 时间： 2018/7/10 0010-上午 11:41
 * 描述： recyclerView方式加载img标签
 * 来源：
 */
public class RecycleViewForHtmlActivity extends AppCompatActivity {
    private BaseQuickAdapter<String, BaseViewHolder> adapter;
    private RecyclerView rv;
    private String content;
    private String imgUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_html);
        initView();
    }

    private void initView() {
        rv = findViewById(R.id.rv_detail);


        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_detail, null) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                GlideUtils.loadImgByWrap(RecycleViewForHtmlActivity.this, item, (ImageView) helper.getView(R.id.iv_detail));
            }
        };
        rv.setLayoutManager(new LinearLayoutManager(RecycleViewForHtmlActivity.this));
        rv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        imgUrl = "<img src=\"/images/image/20161104/20161104144203_82117.jpg\" alt=\"\" /><img src=\"/images/image/20161116/20161116165842_88829.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144206_17003.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144207_62404.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144208_78881.jpg\" alt=\"\" />";
        setHtml(imgUrl);
    }

    /**
     * 设置html
     *
     * @param imgUrl
     */
    private void setHtml(final String imgUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                content = imgUrl.replaceAll("src=\"", "src=\"https://www.38.hn");
                String[] ht = content.split("\"");
                List<String> list = new ArrayList<>();

                for (String rx : ht) {
                    if (rx.contains("https")) {
                        list.add(rx);
                    }

                }
                adapter.setNewData(list);
            }
        }).start();


    }
}
